package com.qf.dao.impl;


import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName a
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/30 15:55
 * @Version 1.0
 **/
@Repository(value = "accountDaoImpl")
public class AccountDaoImpl  implements AccountDao {

    @Autowired
    private Connection conn;

    public List<Account> findAllAccount() {
//        System.out.println(conn+"------------");
        PreparedStatement statement = null;
        try {
            statement = conn.prepareStatement("select * from account");
            ResultSet resultSet = statement.executeQuery();
            List<Account> list=new ArrayList<>();
            while (resultSet.next()) {
                Account account = new Account();
                account.setId(resultSet.getInt(1));
                account.setName(resultSet.getString(2));
                account.setMoney(resultSet.getDouble(3));
                list.add(account);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                statement.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

//    public Account findAccountById(Integer accountId) {
//        try{
//            return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);
//        }catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public void saveAccount(Account account) {
//        try{
//            runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
//        }catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public void updateAccount(Account account) {
//        try{
//            runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
//        }catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public void deleteAccount(Integer accountId) {
//        try{
//            runner.update("delete from account where id=?",accountId);
//        }catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
}

